package com.vehicle_information_management_system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle_information_management_system.dto.EmployeeCount;
import com.vehicle_information_management_system.pojo.DriverInfo;
import com.vehicle_information_management_system.service.DriverInfoService;
import com.vehicle_information_management_system.mapper.DriverInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
* @author huawei
* @description 针对表【driver_info】的数据库操作Service实现
* @createDate 2025-02-16 11:47:13
*/
@Service
public class DriverInfoServiceImpl extends ServiceImpl<DriverInfoMapper, DriverInfo>
    implements DriverInfoService{

    @Autowired
    private DriverInfoMapper driverInfoMapper;
    @Override
    public IPage<DriverInfo> listPage(Integer pageNum, Integer pageSize, String search) {
        Page<DriverInfo> page = new Page<>(pageNum, pageSize);
        QueryWrapper<DriverInfo> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(search)){
            queryWrapper.like("name",search).or().like("phone",search)
                    .or().like("id_number",search).or().like("address",search)
                    .or().like("age",search).or().like("gender",search).or().like("de.department_name",search);
        }
        return driverInfoMapper.getDriverInfoWithDepartment(page,queryWrapper);
    }

    @Override
    public void add(DriverInfo driverInfo) {
        driverInfoMapper.insert(driverInfo);
    }

    @Override
    public void delete(Integer id) {
        driverInfoMapper.deleteById(id);
    }

    @Override
    public void update(DriverInfo driverInfo) {
        driverInfoMapper.updateDriverInfo(driverInfo);
    }

    @Override
    public List<DriverInfo> getDriverInfo() {
        QueryWrapper<DriverInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("name","driver_id","phone");
        return driverInfoMapper.selectList(queryWrapper);
    }

    @Override
    public List<EmployeeCount> getDriverInfoByDepartment() {
        return driverInfoMapper.getDriverInfoByDepartment();
    }

    @Override
    public List<EmployeeCount> getEmployeeCountByAge() {
        return driverInfoMapper.getEmployeeCountByAge();
    }

    @Override
    public Integer getDriverCount() {
        return Math.toIntExact(driverInfoMapper.selectCount(new QueryWrapper<DriverInfo>().eq("1", 1)));
    }
}




